package com.jxb.dialog.utils;


import android.content.Context;
import android.text.InputType;
import android.text.method.ReplacementTransformationMethod;
import android.view.inputmethod.InputMethodManager;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.jxb.dialog.BaseDialog;
import com.jxb.dialog.R;


/**
 * @author jinxb
 * date     : 2021/5/7
 * desc     :
 */
public class InputDialogUtils {
    private InputDialogUtils(){

    }

    public static InputDialogUtils getInstance(){
        return Holder.sInstance;
    }

    private static class Holder{
        private static final InputDialogUtils sInstance = new InputDialogUtils();
    }



    private InputDialog inputDialog;

    /**
     * 输入框dialog（文本,不需要转大写）
     */
    public void showInputDialogText(Context context, String title, String content,
                                    BaseDialog.OnInputBtnClickListener onConfirmListener) {
        showInputDialog(context, title, content, InputType.TYPE_CLASS_TEXT,false, onConfirmListener);
    }
    /**
     * 输入框dialog（文本,需要转大写）
     */
    public void showInputDialogUppercaseText(Context context, String title, String content,
                                    BaseDialog.OnInputBtnClickListener onConfirmListener) {
        showInputDialog(context, title, content, InputType.TYPE_CLASS_TEXT, true,onConfirmListener);
    }

    /**
     * 输入框dialog（int）
     */
    public void showInputDialogInt(Context context, String title, String content,
                                   BaseDialog.OnInputBtnClickListener onConfirmListener) {
        showInputDialog(context, title, content, InputType.TYPE_CLASS_NUMBER, false,onConfirmListener);
    }

    /**
     * 输入框dialog（小数）
     */
    public void showInputDialogFloat(Context context, String title, String content,
                                     BaseDialog.OnInputBtnClickListener onConfirmListener) {
        showInputDialog(context, title, content,
                InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER,false, onConfirmListener);
    }


    public void showInputDialog(Context context, String title, String content, int inputType,boolean isNeedUppercase,
                                BaseDialog.OnInputBtnClickListener onConfirmListener) {
        inputDialog = new InputDialog(context, title, content, inputType, isNeedUppercase,onConfirmListener);
        inputDialog.show();
    }

    public void dismiss() {
        if (inputDialog != null && inputDialog.isShowing()) {
            inputDialog.dismiss();
        }
    }

    private class InputDialog extends BaseDialog {
        private final boolean isNeedUppercase;
        private String title = "";
        private String content = "";
        private int inputType;
        private final OnInputBtnClickListener onConfirmListener;
        private AppCompatTextView titleTextView;
        private AppCompatTextView cancelTextView;
        private AppCompatTextView confirmTextView;
        private AppCompatEditText contentEditText;

        public InputDialog(Context context, String title, String content, int inputType,boolean isNeedUppercase,
                           OnInputBtnClickListener onConfirmListener) {
            super(context);
            this.title = title;
            this.content = content;
            this.isNeedUppercase = isNeedUppercase;
            this.inputType = inputType;
            this.onConfirmListener = onConfirmListener;

        }

        @Override
        protected int getContentView() {
            return R.layout.dialog_input;
        }

        @Override
        public void initViews() {
            titleTextView = findViewById(R.id.titleTextView);
            contentEditText = findViewById(R.id.contentEditText);
            cancelTextView = findViewById(R.id.cancelTextView);
            confirmTextView = findViewById(R.id.confirmTextView);
            contentEditText.setInputType(inputType);
            contentEditText.post(() -> {
                InputMethodManager inputMethodManager =
                        (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            });
            if (isNeedUppercase){
                contentEditText.setTransformationMethod(new ReplacementTransformationMethod() {
                    @Override
                    protected char[] getOriginal() {
                        return new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
                                'm', 'n', 'o',
                                'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
                    }

                    @Override
                    protected char[] getReplacement() {
                        return new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
                                'M', 'N', 'O',
                                'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
                    }
                });
            }
        }

        @Override
        public void initListeners() {
            cancelTextView.setOnClickListener(v -> {
                KeyboardUtils.hideSoftInput(v);
                dismiss();
            });
            confirmTextView.setOnClickListener(view1 -> {
                KeyboardUtils.hideSoftInput(view1);
                String inputStr = contentEditText.getText().toString().trim();
                if (onConfirmListener != null) {
                    onConfirmListener.onClick(inputStr);
                }
                dismiss();
            });
        }

        @Override
        public void show() {
            super.show();
            titleTextView.setText(title);
            contentEditText.setText(content);
            contentEditText.setSelection(content.length());
        }
    }
}
